package gpl.scotlandyard.ui.dialogs;

import gpl.scotlandyard.beans.adapters.TicketsBoardTM;
import gpl.scotlandyard.utils.Config;
import gpl.scotlandyard.utils.I18n;
import gpl.scotlandyard.utils.ImageProvider;

import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.apache.log4j.Logger;

/**
 * Dialog that shows TicketsBoardTM.
 * @author Norbert Martin
 */
public class TicketsBoardDialog {

  private static final Logger _log = Logger.getLogger(TicketsBoardDialog.class);
  private final JDialog jdialog = new JDialog();
  private final TicketsBoardTM model;
  private boolean isInit;

  /** Creates a dialog with this TicketsBoardTM.
   * @param dtm
   */
  public TicketsBoardDialog(TicketsBoardTM dtm) {
    dtm.getClass();
    model = dtm;
  }

  /** Sets visible the dialog.
   * @param b
   */
  public void setVisible(boolean b) {
    if (b && !isInit) {
      init();
    }
    if (b) {
      _log.info("game board view showed");
    } else {
      _log.info("game board view hidden");
    }
    jdialog.setVisible(b);
  }

  /**
   * Initializes the dialog.
   */
  private void init() {
    jdialog.setIconImage(ImageProvider.get(Config.get("TICKET_ICO")).getImage());
    jdialog.setLocationByPlatform(true);
    jdialog.setAlwaysOnTop(true);
    jdialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    jdialog.setTitle(I18n.get("PLAYER_BOARD"));
    JTable table = new JTable(model);
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    jdialog.getContentPane().add(new JScrollPane(table));
    jdialog.pack();
  }
}
